import cv2
import numpy as np

def detect(readimg):
    gray = cv2.cvtColor(readimg, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 21, 75, 75) #双边滤波消除噪声
    gray = cv2.medianBlur(gray, 7)
    
    # Canny 边缘检测
    canny = cv2.Canny(gray, 50, 150)

    # 圆形检测
    circles = cv2.HoughCircles(
        canny, cv2.HOUGH_GRADIENT, 1, 30, param1=50, param2=20, minRadius=10, maxRadius=30)

    if np.any(circles != None):
        # 对数据进行四舍五入变为整数
        circles = np.uint16(np.around(circles))
    else:
        circles = np.array([[[0, 0, 0]]])

    choose = circles[0, :]

    r, x, y = (choose[0, 2]), (choose[0, 0]), (choose[0, 1])
    return r, x, y

